# -*- coding: utf-8 -*-
from django.http import HttpResponse, HttpResponseRedirect
from apodeiksi.apodeiksiapp import models
from apodeiksi.apodeiksiapp import my
from google.appengine.api import users
import forms
from django.shortcuts import render_to_response
import csv

def render(request, template, payload):
	my.debug("clear")
	payload['login'] = users.create_login_url("/")
	payload['logout'] = users.create_logout_url("/")
	payload['user'] = users.get_current_user()
	if payload['user'] is None:
		my.sessionUser('clear')	
	else:
		user_query = models.Users.all().filter('googleProperties = ', payload['user']).get()
		if user_query == None:
			newUser = models.Users()
			newUser.googleProperties  = payload['user']
			newUser.put()
	response = render_to_response(template, payload)
	return response

def csvTest(request):
	# Create the HttpResponse object with the appropriate CSV header.
	currentUser = users.get_current_user()
	creator = models.Users.all().filter("googleProperties = ", currentUser).get()
	receiptList = models.Receipts.all().filter("creator = ", creator).fetch(9999)
	response = HttpResponse(mimetype='text/csv')
	response['Content-Disposition'] = 'attachment; filename='+'αποδείξεις'+'.csv'
	writer = csv.writer(response)
#	writer.writerow(['�.�.�', '����', '����������', '���������'])
	row = []
	for receipt in receiptList: 
		row.append(str(receipt.afm).encode('utf-8'))
		row.append(str(receipt.amount))
		row.append(str(receipt.date))
#		row.append(str(receipt.storeDescription).encode('utf-8'))
		writer.writerow(row)

	return response

def main(request , key = None):
	mode = None
	if request.method == 'POST':
		if request.POST['mode'] == 'edit':
			editedReceipt = models.Receipts.get(request.POST['key'])
			receiptform = forms.ReceiptForm(request.POST , instance = editedReceipt)
		else:
			receiptform = forms.ReceiptForm(request.POST)
		if receiptform.is_valid(): # All validation rules pass
			receiptform.save()
	if key:
		receiptform = forms.ReceiptForm(instance = models.Receipts.get(key))
		mode = 'edit'
	else:
		receiptform = forms.ReceiptForm()		
	currentUser = users.get_current_user()
	creator = models.Users.all().filter("googleProperties = ", currentUser).get()
	receiptList = models.Receipts.all().filter("creator = ", creator).fetch(10)
	my.debug("creator = " + str(creator) +"   "+ "List: "+ str(receiptList))	
	payload = dict(receiptform=receiptform, receiptList=receiptList, user = currentUser, mode = mode, key = key, debug=my.debug())
	return render(request, 'main.html', payload)

def receiptsList(request):
	currentUser = users.get_current_user()
	creator = models.Users.all().filter("googleProperties = ", currentUser).get()
	receiptList = models.Receipts.all().filter("creator = ", creator).fetch(10)
	payload = dict(receiptList=receiptList, user = currentUser , debug=my.debug())
	return render(request, 'receiptsList.html', payload)
#def user(request,user):
#	loggedUser = users.get_current_user()
#	models.Receipts.creator
#	user_query = models.Users.all().filter('googleProperties = ', users.get_current_user()).fetch(1000)
#	if len(user_query) == 0:
#		payload= dict(message='User does not exist')
#		return render(request, 'error.html', payload)
#	userEvents = models.UserInEvent.all().filter('user = ' , user_query[0]).fetch(1000)
#	for i in userEvents:
#		events[i] = userEvents[i].event
#	payload(events = events)
#	return render(request, 'events.html', payload)

def thanks(request):

	payload = dict()
	return render(request, 'thanks.html', payload)
